<?php
/**
 * 用户基本资料等信息操作 业务层
 * @author yangchao
 * 包括用户登录，用户密码加密，用户cookie验证和加密，都在本业务逻辑定义
 * @package user
 * @copyright yangchao
 * @version 1.0
 * @code-encode utf-8
 * @data-encode 2010-01-17
 */

class user_info {
	
	/**
	 * 当前登录用户的ID
	 *
	 * @var int
	 */
	private $_cur_uid = "";
	
	private $secret_key = "yangchao";
	
	private $_cur_user_info = array();
		

	/**
	 *  根据UID取用户详细信息
	 * @param unknown_type $uid
	 */
	function get_user_info_by_uid($uid)
	{
		/*$db_user_info = new db_user_info();
		
		$user_detai_info = $db_user_info->get_user_info_item_by_uid($uid);
		if($user_detai_info)
		{
			$user_detai_info["user_pic"] = "http://tp1.sinaimg.cn/{$uid}/180/5635936750/1";
			$user_detai_info["user_pic1"] = str_replace("/180/","/50/",$user_detai_info["user_pic"]);
			$user_detai_info["user_pic2"] = str_replace("/180/","/30/",$user_detai_info["user_pic"]);
		}
		else
		{
			$this->add_new_user($uid);
		}
		*/
		$db_qq_user_info = new db_qq_user_info();
		$user_detai_info = $db_qq_user_info->get_qq_user_info_item_by_uid($uid);
		
		
		return $user_detai_info;
	}
	
	
	function get_user_info_by_sina_uid($current_uid)
	{
		/*$db_user_info = new db_user_info();
		
		$user_detai_info = $db_user_info->get_user_info_item_by_uid($uid);
		if($user_detai_info)
		{
			$user_detai_info["user_pic"] = "http://tp1.sinaimg.cn/{$uid}/180/5635936750/1";
			$user_detai_info["user_pic1"] = str_replace("/180/","/50/",$user_detai_info["user_pic"]);
			$user_detai_info["user_pic2"] = str_replace("/180/","/30/",$user_detai_info["user_pic"]);
		}
		else
		{
			$this->add_new_user($uid);
		}
		*/
		$db_qq_user_info = new db_qq_user_info();
		$user_detai_info = $db_qq_user_info->get_qq_user_info_item_by_openid($current_uid);
		if($user_detai_info)
		{
			
		}
		else
		{
			$uid = $this->add_new_user_sina($current_uid);
			$user_detai_info = $db_qq_user_info->get_qq_user_info_item_by_uid($uid);
			
		}
		
		
		return $user_detai_info;
	}
	
	
	function add_new_user_sina($current_uid)
	{
		$mblog_from_api = new mblog_from_api();
		$mblog_info = $mblog_from_api->api_get_user_info_by_uid($current_uid);
		$mblog_info = $mblog_info["data"]["result"];
			
		$db_qq_user_info = new db_qq_user_info();
		if($mblog_info["gender"] == "f")
		{
			$sex=2;
		}
		elseif($mblog_info["gender"] == "m")
		{
			$sex=1;
		}
		else
		{
			$sex=0;
		}
		
		$user_nickname = $mblog_info["name"];
		$province = $mblog_info["province"];
		$city = $mblog_info["city"];
		$head_url = $mblog_info["profile_image_url"];
		
		$user_list = $mblog_from_api->api_get_user_age_by_uids($current_uid);
		
		$education_array = array();
		foreach($user_list["data"] as $key => $value)
		{
			$education_array[$key] = $value;
		}
		
		if($education_array[$current_uid])
		{
			$max_info = array();
			$max_year =0;
			//type学校类型，1：大学、2：高中、3：中专技校、4：初中、5：小学；
			$school_type = "";
			foreach($education_array[$current_uid]["education"] as $key1 => $value1)
			{
				if($value["year"] >=  $max_year)
				{
					$max_year = $value1["year"];
					$max_info = $value1;
					$school_type = $value1["type"];
				}
			}
			
			$school_id = $max_info["school_id"];
			$school_name = mysql_escape_string($max_info["school"]);
			$start_year = $max_info["year"];
			
			
			$birthyear = substr($education_array[$current_uid]["profile_info"]["birthday"],0,4);
			
			
			
			if((!is_numeric($birthyear) || intval($birthyear) < 1900) &&  $start_year)//如果不是数字，表示不是年份
			{
				if($school_type == 1)
				{
					$birthyear = $start_year - 18;
				}
				elseif($school_type == 2)
				{
					$birthyear = $start_year - 15;
				}
				elseif($school_type == 3)
				{
					$birthyear = $start_year - 15;
				}
				elseif($school_type == 4)
				{
					$birthyear = $start_year - 12;
				}
				elseif($school_type == 5)
				{
					$birthyear = $start_year - 7;
				}
				else
				{
					$birthyear=0;
				}
				
			}
			else
			{
				if(intval($birthyear)<1900)
				{
					$birthyear = 0 ;
				}
						
				
			}
		}
		
		$company ="";
		$school= $school_id;
		$verified_type = $mblog_info["verified_type"];
		
		$res = $db_qq_user_info->insert_qq_user_info($current_uid,$sex,$user_nickname,$province,$city,$company,$school,$verified_type,$education_array[$current_uid]["profile_info"]["birthday"],$school_name,$start_year,$birthyear,$head_url);
		
		if($res)
		{
			$last_insert_id = $db_qq_user_info->get_lastinsertid();
			return $last_insert_id;
		}
		else
		{
			return false;
		}
	}
	
	function get_user_info_by_openid($openid)
	{
		/*$db_user_info = new db_user_info();
		
		$user_detai_info = $db_user_info->get_user_info_item_by_uid($uid);
		if($user_detai_info)
		{
			$user_detai_info["user_pic"] = "http://tp1.sinaimg.cn/{$uid}/180/5635936750/1";
			$user_detai_info["user_pic1"] = str_replace("/180/","/50/",$user_detai_info["user_pic"]);
			$user_detai_info["user_pic2"] = str_replace("/180/","/30/",$user_detai_info["user_pic"]);
		}
		else
		{
			$this->add_new_user($uid);
		}
		*/
		$db_qq_user_info = new db_qq_user_info();
		$user_detai_info = $db_qq_user_info->get_qq_user_info_item_by_openid($openid);
		if($user_detai_info)
		{
			
		}
		else
		{
			$uid = $this->add_new_user_qq($openid);
			$user_detai_info = $db_qq_user_info->get_qq_user_info_item_by_uid($uid);
			
		}
		
		
		return $user_detai_info;
	}
	
	function add_new_user_qq($openid)
	{
		
		$mblog_get_mblog_from_qq = new mblog_get_mblog_from_qq();
		$user_info = $mblog_get_mblog_from_qq->get_user_info_by_uid($openid);
		
	
		$birthyear = $user_info["data"]["birth_year"];
		$birthday  = $user_info["data"]["birth_month"]."-".$user_info["data"]["birth_day"];
		if($user_info["data"]["edu"])
		{
			
			//type学校类型，<option value="6">博士</option><option value="5">硕士</option><option selected="" value="4">大学</option><option value="3">高中</option><option value="2">初中</option><option value="1">小学</option>
			$max_level =0;
			$get_edu ="";
			foreach($user_info["data"]["edu"] as $key => $value)
			{
				if($max_level < $value["level"])
				{
					$get_edu = $value;
				}
			}
			$start_year = $get_edu["year"];
			$school_id =  $get_edu["schoolid"];
			$school_type =  $get_edu["level"];
			if((!is_numeric($birthyear) || intval($birthyear) < 1900) &&  $start_year)//如果不是数字，表示不是年份
			{
				if($school_type == 6)
				{
					$birthyear = $start_year - 25;
				}
				elseif($school_type == 5)
				{
					$birthyear = $start_year - 22;
				}
				elseif($school_type == 4)
				{
					$birthyear = $start_year - 18;
				}
				elseif($school_type == 3)
				{
					$birthyear = $start_year - 15;
				}
				elseif($school_type == 2)
				{
					$birthyear = $start_year - 12;
				}
				elseif($school_type == 1)
				{
					$birthyear = $start_year - 7;
				}
				else
				{
					$birthyear=0;
				}
				
			}
			else
			{
				if(intval($birthyear)<1900)
				{
					$birthyear = 0 ;
				}
						
				
			}
		}
		
		$company ="";
		$school= $school_id;
		$verified_type = $mblog_info["isvip"];
		$city = $user_info["data"]["city_code"];
		$user_nickname =  $user_info["data"]["nick"];
		$sex =  $user_info["data"]["sex"];
		
		$link =  $user_info["data"]["name"];

		if(!$user_info["data"]["head"])
		{
			$head_url ="http://mat1.gtimg.com/www/mb/img/p1/head_normal_180.png";
		}
		else
		{
			$head_url = $user_info["data"]["head"]."/50/";
		}
		$db_qq_user_info = new db_qq_user_info();
		$res = $db_qq_user_info->insert_qq_user_info($openid,$sex,$user_nickname,$province,$city,$company,$school,$verified_type,$birthday,$school_name,$start_year,$birthyear,$head_url,$link);
		
		if($res)
		{
			$last_insert_id = $db_qq_user_info->get_lastinsertid();
			return $last_insert_id;
		}
		else
		{
			return false;
		}
		
		
	}
	/**
	 *  根据用户名和密码取用户详细信息
	 * @param unknown_type $uid
	 */
	function get_user_item_by_name_and_password($user_name,$password)
	{
		$db_user_info = new db_user_info();
		$md5_password = $this->encryption($password);
		$user_detai_info = $db_user_info->get_user_item_by_name($user_name);
		
		//check password
		if($user_detai_info["password"] == $md5_password)
		{
			return $user_detai_info;
		}
		else
		{
		return false;
		}
	}
	
	/**
	 * 密码加密方式
	 * @param unknown_type $password
	 */
	function encryption($password)
	{
		return md5($password);
	}
	
	/**
	 * 
	 * @param array $user_detail_info
	 */
	function set_cookie_by_user_info($uid,$user_nickname)
	{
		
		$this->drop_cookie();
		
		$cookie_info = "uid={$uid}&user_name={$user_nickname}";
		
		$cookie_key = $this->cookie_encryption($cookie_info);
		
		setcookie("GINFO",$cookie_info, time()+3600*24,"/",COOKIE_DOMAIN);
		setcookie("GKEY",$cookie_key, time()+3600*24,"/",COOKIE_DOMAIN);
     
		return true;
	}
	
	
	
	/**
	 * cookie加密方式
	 * @param unknown_type $password
	 */
	function cookie_encryption($cookie_info)
	{
		return md5($cookie_info).md5($this->secret_key);
	}
	
	/**
	 * 删除cookie信息
	 */
	function drop_cookie()
	{
	
		session_unset();
	
		setcookie("GINFO","", 0,"/",COOKIE_DOMAIN);
		setcookie("GKEY","",0,"/",COOKIE_DOMAIN);
		
		
		return true;
	}
	
	
	/**
	 * 判断是否已经登陆
	 */
	function is_logined()
	{
		if(!isset($_COOKIE["GKEY"]) || !isset($_COOKIE["GINFO"]))
		{
			return false;
		}
		
		$compare_key = $this->cookie_encryption($_COOKIE["GINFO"]);
		$cookie_info = $_COOKIE["GINFO"];
		parse_str($cookie_info);
			
		if($_COOKIE["GKEY"] ==  $compare_key && $uid)
		{
			return true;
		}
		else
		{
			return false;
		}
	}


	
	/**
	 * 获取当前登录用户信息
	 */
	function get_cur_user_info()
	{
		if($this->is_logined())
		{
			$uid = $this->get_cur_uid();
			$user_detail_info = $this->get_user_info_by_uid($uid);	
			return $user_detail_info;		
		}
		else
		{
			return false;
		}
	
	}
	
	//当前登录UID
	function get_cur_uid()
	{
		if(!$this->is_logined())
		{
			return false;
		}
		if(!$this->_cur_uid )
		{
			$cookie_info = $_COOKIE["GINFO"];
			parse_str($cookie_info);
			
			$this->_cur_uid = $uid;
		}	
		return $this->_cur_uid;
	}
	
	

	static function check_super_admin()
	{
		
		$super_admin = array(1737886622);
		$user_info = new user_info();
		if(!$user_info->is_logined())
		{
			return false;
		}
		$uid = $user_info->get_cur_uid();
		if(in_array($uid,$super_admin))
		{
			return $uid;
		}
		else
		{
			return false;
		}
	
	}
	
	
	function update_user_mblog_count_and_main_topic($uid,$value)
	{
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$db_base = new db_base();
		$sql = "select count(topic_id) as count,topic_id from mblog_pic_content where create_uid = {$uid}  group by topic_id";
		$list = $db_base->select($sql);
		$sql = "delete from user_search  where uid = $uid";
		$db_base->query($sql);
		
		$max_value = 0;
		$count= 0;
		$main_topic_id =0;
		foreach($list as $key => $value2)
		{
			$sql = "replace into user_search (uid,user_nickname,verified_type,sex,birthyear,school_id,school_name,start_year,province,city,location,birthday,topic_id,topic_count) values
			('{$value["uid"]}','{$value["user_nickname"]}','{$value["verified_type"]}','{$value["sex"]}','{$value["birthyear"]}','{$value["school_id"]}','{$value["school_name"]}','{$value["start_year"]}','{$value["province"]}',
			'{$value["city"]}','{$value["location"]}','{$value["birthday"]}','{$value2["topic_id"]}','{$value2["count"]}') ";
			$db_base->query($sql);
			
			if($max_value < $value2["count"])
			{
				$max_value = $value2["count"];
				$main_topic_id = $value2["topic_id"];
				
			}
			$count +=$value["count"];
		}
		
		if($main_topic_id)
		{
			$topic_info = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_item_by_id($main_topic_id);
			$main_topic = $topic_info["topic"];
		}
		$sql ="select count(*) as count from mblog_pic_content where create_uid = $uid and status =1 ";
		$get_count= $db_base->select($sql);	
		$count_mblog = $get_count[0]["count"];
		
		$sql = "update qq_user_info set count_mblog = $count_mblog,main_topic_id=$main_topic_id,main_topic='{$main_topic}' where uid = '{$uid}'";
		$res = $db_base->query($sql);
		
		
	}
	
}
	